
import { Component, OnInit, ViewChild } from '@angular/core';
//增删改查共通Service
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';

@Component({
  selector: 'app-report-lower-cylinder-assembly',
  templateUrl: './lower-cylinder-assembly.component.html',
  styleUrls: ['./lower-cylinder-assembly.component.scss']
})

export class LowerCylinderAssemblyComponent implements OnInit {
  // 监控数据
  monitorData: any = {
    offlineCurrentHour: '',
    offlineMachine: '',
    offlinePreHour: '',
    offlineTotal: '',
    onlineCurrentHour: '',
    onlinePreHour: '',
    onlineTotal: '',
    targetProduction: '',
    stationList: []
  };
  stationNum: string;
  stationInfo: any = {
    blockTime: '',
    code: '',
    interruptTime: '',
    jph: '',
    model: '',
    palletNum: '',
    production: '',
    review: '',
    status: '',
    waitTime: ''
  };
  // 定时器
  set = null;
  // 工位列表DOM元素
  stationListEle: any[];
  constructor(private commServices: CommService) {
  }
  // 组件初始化
  ngOnInit() {
    // 默认选中首工位
    this.stationNum = '1010';
    // // test
    // this.stationNum = '050A';
    this.getReportData();
    this.set = setInterval(() => this.getReportData(), 30 * 1000);
  }

  /**
   * 获取表格数据
   */
  getReportData(): void {
    const queryModel: QueryModel = new QueryModel();
    // 下缸体拓展总成：C5LA1
    queryModel.querySubAreaIds = ['C5LA1'];
    this.commServices
      .getList(queryModel, ReportUrls.MonitorData)
      .subscribe(model => {
        if (model.resultCode === 200) {
          // this.ctsList = model.data;
          // console.log(model.data);
          this.monitorData = this.commServices.formatMonitorData(model.data);
          // 更新当前工位信息
          this.stationClick(this.stationNum);
          // console.log(this.monitorData);
        } else {
          console.log(model.resultCode + ',' + model.resultMessage);
        }
      });
  }
  formatStationColor(stationNum: string) {
    const stationInfo = this.monitorData.stationList.find(a => a.code === stationNum);
    if (stationInfo) {
        return stationInfo.statusColor;
    } else {
        // 默认运行状态
        return '#05fb0b';
    }
}
  // 工位点击事件
  stationClick(stationNum) {
    this.stationNum = stationNum;
    // console.log('event', stationNum);
    const stationInfo = this.monitorData.stationList.find(a => a.code === stationNum);
    if (stationInfo) {
      this.stationInfo = stationInfo;
    } else {
      this.stationInfo = {
        blockTime: '',
        code: this.stationNum,
        interruptTime: '',
        jph: '',
        model: '',
        palletNum: '',
        production: '',
        review: '',
        status: '',
        waitTime: ''
      };
    }
  }
  // 格式化工位故障时间
  formatFaultTime(time: string) {
    if (time === '') {
      return '0';
    } else {
      const timeNum = Number(time);
      return Math.floor(timeNum / 60) + '分' + (timeNum % 60) + '秒';
    }
  }
}
